package cn.edu.zafu.bigdata.x.YinMingyan;

import java.util.Random;  // 导入Random类，用于生成随机数
import java.util.Scanner;  // 导入Scanner类，用于读取游戏玩家的输入

public class GuessNumber {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int maxGuesses = 10; // 最大猜测次数
        int numberToGuess = generateRandomNumber(random, 1, 50); // 生成1到50之间的随机数

        boolean gameRunning = true; // 游戏运行状态标志

        while (gameRunning) {
            System.out.println("欢迎玩家进入猜数字游戏，快来猜测数字吧！");
            System.out.println("这个数是一个整数，取值在1到50之间，你有" + maxGuesses + "次机会猜中它。");

            for (int guessesTimes = 1; guessesTimes <= maxGuesses; guessesTimes++) {
                System.out.print("你猜测的数字是: ");
                int guess = scanner.nextInt();

                if (guess == numberToGuess) {
                    System.out.println("哇，666！你猜对了，数字是 " + numberToGuess);
                    System.out.println("游戏胜利");
                    break;
                } else if (guessesTimes < maxGuesses) {
                    if (guess < numberToGuess) {
                        System.out.println("猜测的数字太小了，往大的数试试。");
                    } else if (guess > numberToGuess) {
                        System.out.println("猜测的数字太大了，往小的数试试。");
                    }
                    System.out.println("你还有 " + (maxGuesses - guessesTimes) + " 次机会。");
                } else {
                    System.out.println("非常可惜，你的机会用完了。数字是 " + numberToGuess);
                    System.out.println("游戏失败");
                }
            }

            System.out.println("你想要再玩一次吗？(yes/no)");
            String playAgain = scanner.next();
            if (!playAgain.equalsIgnoreCase("yes")) {
                gameRunning = false;
            }
        }

        scanner.close();
        System.out.println("感谢你的参与！");
    }

    // 生成指定范围内的随机数
    private static int generateRandomNumber(Random random, int min, int max) {
        return random.nextInt((max - min) + 1) + min;
    }
}